package com.qf.flash.ui.home;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.TimePickerView;
import com.qf.flash.R;
import com.qf.flash.http.response.PlayConditinoResponse;
import com.qf.flash.http.response.PlayTypeBean;
import com.qf.flash.model.Pay2SponsorBean;
import com.qf.flash.model.ScriptRoomBean;
import com.qf.flash.mvp.contract.ScriptContract;
import com.qf.flash.mvp.presenter.ScriptPrensenter;
import com.qf.flash.ui.home.adapter.NumberItem;
import com.qf.flashlib.adapter.RVAdapter;
import com.qf.flashlib.app.FlashLib;
import com.qf.flashlib.base.BaseActivity;
import com.qf.flashlib.util.DateUtil;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.qf.flashlib.util.DateUtil.PATTERN_STANDARD16H;

/**
 * 2020/3/4 15:15
 * 狼人杀
 */
public class WolfKillActivity extends BaseActivity implements ScriptContract.View {
    @BindView(R.id.et_group_name)
    EditText etGroupName;
    @BindView(R.id.tv_date)
    TextView tvDate;
    @BindView(R.id.tv_top_title)
    TextView tvTopTitle;
    @BindView(R.id.et_password)
    EditText etPassword;
    @BindView(R.id.rv_count)
    RecyclerView rvCount;

    private RVAdapter<String> mNumberAdapter;
    private TimePickerView mTimePicker;
    private Date mSelectDate;
    private PlayTypeBean playTypeBean;
    private ScriptPrensenter mScriptPresenter;


    public static void start(Activity activity, PlayTypeBean playType) {
        Intent intent = new Intent(activity, WolfKillActivity.class);
        intent.putExtra("playType", playType);
        activity.startActivity(intent);
    }

    @Override
    protected void initInject() {
        mScriptPresenter = new ScriptPrensenter();
        initPresenters(mScriptPresenter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_wolf_kill;
    }

    @Override
    protected void initViewAndData() {
        playTypeBean = getIntent().getParcelableExtra("playType");
        tvTopTitle.setText(playTypeBean.getName());
        initRvNumber();
        //初始化时间
        initTimeDialog();
        mScriptPresenter.getConditions(playTypeBean.getTemplateId(), "0");
    }


    private void initRvNumber() {
        LinearLayoutManager manager = new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.HORIZONTAL);
        rvCount.setLayoutManager(manager);
        mNumberAdapter = new RVAdapter<>(new NumberItem());
        mNumberAdapter.setOnItemClickListener(new RVAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                mNumberAdapter.setSingleChosenPosition(position);
                mNumberAdapter.notifyDataSetChanged();
            }
        });
        rvCount.setAdapter(mNumberAdapter);
    }

    /**
     * 弹出选择时间
     */
    private void initTimeDialog() {
        //标题颜色
        int color = getResources().getColor(R.color.color_333333);
        //选中的时间
        Calendar selectedDate = Calendar.getInstance();
        if (mSelectDate != null)
            selectedDate.setTime(mSelectDate);
        //开始时间
        Calendar startDate = Calendar.getInstance();
        //结束时间
        Calendar endDate = Calendar.getInstance();
        endDate.set(2100, 11, 31);
        //构建选择器
        mTimePicker = new TimePickerBuilder(this, mTimeListener)
                .setType(new boolean[]{true, true, true, true, true, false})  // 默认全部显示
                .setOutSideCancelable(true)                                     //点击屏幕，点在控件外部范围时，是否取消显示
                .isCyclic(false)                                                //是否循环滚动
                .setSubmitColor(color)                                          //确定按钮文字颜色
                .setCancelColor(color)                                          //取消按钮文字颜色
                .setDate(selectedDate)                                          // 如果不设置的话，默认是系统时间*/
                .setRangDate(startDate, endDate)
                .setLabel("年", "月", "日", "时", "分", "秒")                     //默认设置为年月日时分秒
                .isCenterLabel(false)                                            //是否只显示中间选中项的label文字，false则每项item全部都带有label。
                .isDialog(false)                                                //是否显示为对话框样式
                .build();
    }

    /**
     * 选择时间监听
     */
    private OnTimeSelectListener mTimeListener = new OnTimeSelectListener() {
        @Override
        public void onTimeSelect(Date date, View v) {
            //记录时间
            mSelectDate = date;
            tvDate.setText(DateUtil.getDateToString(date.getTime(), PATTERN_STANDARD16H));
        }
    };

    private void sponsorGame() {
        if (TextUtils.isEmpty(etGroupName.getText())) {
            FlashLib.showToast("请输入组队名称");
            return;
        }
        if (TextUtils.isEmpty(tvDate.getText())) {
            FlashLib.showToast("请选择时间");
            return;
        }
        Pay2SponsorDialogFragment.newInstarnce(getSponsorBean())
                .show(getSupportFragmentManager());
    }

    private Pay2SponsorBean getSponsorBean() {
        Pay2SponsorBean bean = new Pay2SponsorBean();
        bean.setTeamName(etGroupName.getText().toString());
        bean.setStartTime(tvDate.getText().toString());
        bean.setMoney(playTypeBean.getDownpayment());
        int position = mNumberAdapter.getSingleChosenPosition();
        if (position != -1) {
            String data = mNumberAdapter.getData(position);
            bean.setNum(data);
        }
        bean.setPassword(etPassword.getText().toString());
        bean.setPlayType(playTypeBean.getId());
        bean.setPlayTypeName(playTypeBean.getName());
        return bean;
    }



    @OnClick({R.id.tv_top_back, R.id.tv_date, R.id.tv_sponsor})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_top_back:
                finish();
                break;
            case R.id.tv_date:
                if (mTimePicker != null)
                    hideInput();
                mTimePicker.show();
                break;
            case R.id.tv_sponsor:
                sponsorGame();
                break;
        }
    }

    @Override
    public void setPlayListData(List<ScriptRoomBean> data, int totalPage) {

    }

    @Override
    public void setConditionData(PlayConditinoResponse response) {
        mNumberAdapter.setDatas(response.getNums());
    }

    protected void hideInput() {
        InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        View v = getWindow().peekDecorView();
        if (null != v) {
            imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
        }
    }
}
